import { Anything } from 'utils/types/any';

// 数组变hash方便索引
export const arrayToHash = <T extends Record<string, any>, K extends keyof T>(
  list: T[],
  key: K,
) => {
  const hashList: { [key: string]: T } = {};
  list.forEach((item) => {
    const tmpKey = String(item[key]);
    hashList[tmpKey] = item;
  });
};
// 数组变hash数组，相同键分一组
export const hashGroup = <T extends Anything, K extends keyof T>(
  list: T[],
  key: K,
) => {
  const hashList: { [key: string]: T[] } = {};
  list.forEach((item) => {
    const groupKey = String(item[key]);
    if (!hashList[groupKey]) hashList[groupKey] = [];
    hashList[groupKey].push(item);
  });
  return hashList;
};
